<template>
  <CompOperate
    :field="field"
    :designer="designer"
    :design-state="designState"
    :parent-list="parentList"
    :parent-widget="parentWidget"
    :sub-form-row-id="subFormRowId"
    :sub-form-row-index="subFormRowIndex"
    :sub-form-col-index="subFormColIndex"
    :index-of-parent-list="indexOfParentList"
    :display-style="field.options.displayStyle"
  >
    <div class="scene-report-pick-info-widget" :style="getStyle('head')">
      <div class="pick-area-wrap">
        <div class="pick-area">
          <div class="top-header">
            <div class="left">
              <img
                :src="require(`@/components/assets/images/design/1-12.webp`)"
              />
            </div>
            <div class="middle" :style="getStyle('content')">
              {{ optionsInfo.content}}
            </div>
            <div class="right">
              <img
                :src="require(`@/components/assets/images/design/1-12.webp`)"
              />
            </div>
          </div>
          <div class="overFillHidden">
            <div class="second" ref="wrap" :data-info="getElements">
              <el-input
                ref="inputField"
                v-model="value"
                :autofocus="true"
                :placeholder="optionsInfo.placeholder"
                @blur="handleFocusInput"
                @change="keyupEnter"
              />
              <!-- @change="handleInput('enter')" -->
            </div>
          </div>
          <div class="pick-type">
            <div
              class="item button-wrap"
              v-for="item in getReportPickInfo"
              :key="item.terminalId"
            >
              <div class="bottom">
                <div class="button" @click="handlePickType(item)">
                  {{ item.title }}
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </CompOperate>
</template>

<script>
import { Icon, Notify } from "vant";
import { pxToVw } from "@/utils/common";
import emitter from "@/utils/designer/emitter";
import { buildParentApi } from "@/utils/designer/util";
import mixin from "@sc/templateDetails/modules/homeMiddle/fieldMixin";
import CompOperate from "@c/public/form-designer/devType/modules/compOperate.vue";

export default {
  name: "scene-report-pick2-info-widget",

  componentName: "FieldWidget", //必须固定为FieldWidget，用于接收父级组件的broadcast事件

  mixins: [emitter, mixin],

  components: {
    Icon,
    CompOperate,
    Notify,
  },

  props: {
    widget: Object,
    field: Object,
    parentWidget: Object,
    parentList: Array,
    indexOfParentList: Number,
    designer: Object,
    designState: {
      type: Boolean,
      default: false,
    },
    subFormRowIndex: {
      /* 子表单组件行索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormColIndex: {
      /* 子表单组件列索引，从0开始计数 */ type: Number,
      default: -1,
    },
    subFormRowId: {
      /* 子表单组件行Id，唯一id且不可变 */ type: String,
      default: "",
    },
    headTitle: {
      type: String,
      default: "",
    },
    visibleTitle: {
      type: Boolean,
    },
    //prop传入的表单JSON配置
    parentApi: {
      type: Object,
      default: () => buildParentApi(),
    },
    devType: {
      type: Boolean,
      default: false,
    },
  },

  data() {
    return {
      step: 0,
      tipsShow: true,
      // 输入的参数
      value: '',
      showInput: true,
      backcountdown: 120000,
      optionsInfo: Object(),
    };
  },

  computed: {
    // 设置的行内样式
    getStyle() {
      return function (val) {
        const self = this;
        let info = self.field?.options?.reportPickInfoConfig || Object();
        let Obj = Object();
        switch (val) {
          case "head":
            Reflect.set(Obj, "backgroundColor", `${info.backgroundColor}`);
            let step = info.step;
            switch (step) {
              case 0:
                Reflect.set(Obj, "height", pxToVw(info.headHeight));
                break;
              case 1:
                let heighe = parseInt(info.headHeight) + 100;
                Reflect.set(Obj, "height", pxToVw(heighe));
                break;
              default:
                Reflect.set(Obj, "height", pxToVw(info.headHeight));
                break;
            }
            return Obj;
          case "content":
            Reflect.set(Obj, "color", `${info.fontColor}`);
            Reflect.set(Obj, "fontSize", `${pxToVw(info.fontSize)}`);
            return Obj;
        }
      };
    },

    getElements() {
      this.$nextTick(() => {
        if (!this.$lodash.isEmpty(this.$refs)) {
          console.log(this.$refs);
          this.$refs.inputField.focus();
          return this.$refs?.inputField;
        } else {
          return null;
        }
      });
    },

    //步距展示对应的组件
    // showComp() {
    //   return function (val) {
    //     return this.optionsInfo.step === val;
    //   };
    // },

    // 获取父级传递过来的step
    // getBackcountdown() {
    //   let { backcountdown } = this.field.options.reportInfoConfig;
    //   return backcountdown * 1000 || 120000;
    // },

    //  取号机业务类型列表
    getReportPickInfo() {
      let devType = this.devType;
      let reportPickInfo = this.$store.getters["getReportPickInfo"];
      let reportPickShowInfo;
      if (devType) {
        reportPickShowInfo = reportPickInfo["apiData"];
      } else {
        reportPickShowInfo = reportPickInfo["mockData"];
      }
      return reportPickShowInfo;
    },
  },

  created() {
    this.registerToRefList();
    this.initEventHandler();
    this.handleOnCreated();
  },

  mounted() {
    this.handleOnMounted();
    this.handleFocusInput();
  },

  methods: {
    handlePickType(row) {
      // 调用父组件的函数--回传数据
      //this.$emit('goToInit', row);
      this.parentApi.executeParentMethod("goToInit", row);
    },
    // c++ 提交号别
    handleHiddenSubmit(params) {
      this.parentApi.executeParentMethod("handleHiddenSubmit", params);
      //  this.parentApi.executeParentMethod('handleReport', params);
    },
    // 接收父组件传递过来的数据
    executeMethod(key, data) {
      const self = this;
      switch (key) {
        case "reportPickInfo":
          self.$store.dispatch("setReportPickInfo", data);
          break;
      }
    },

    handleFocusInput() {
      console.log("失焦");
      const self = this;
      if (self.devType) {
        setTimeout(() => {
          self.$refs.inputField?.focus();
        }, 100);
      }
    },
    keyupEnter(value) {
      const self = this;
      if (self.value.trim().length > 0) {
        let params = Object();
        Reflect.set(params, "number", self.value);
        // self.$emit("handleSubmit", params);
        // self.$emit("handleHiddenSubmit", params);
        self.handleHiddenSubmit(params);
      } else {
        this.errorShow = true;
        console.log("提示错误信息");
      }
      this.value = "";
    },
    handleInput(key) {
      const self = this;
      switch (key) {
        case "del":
          this.value = self.value.slice(0, -1);
          break;
        case "reset":
          this.value = "";
          break;
        // 提交按钮
        case "enter":
          if (self.value.trim().length > 0) {
            let params = Object();
            Reflect.set(params, "number", self.value);
            // self.$emit('handleSubmit', params);
            self.handleHiddenSubmit(params);
            self.value = "";
          }
          break;
        default:
          if (self.value.trim().length < 18) {
            this.value += key;
          }
      }
    },

    // 返回首页
    handleBack() {
      this.optionsInfo.step = 0;
      this.parentApi.executeParentMethod("openReadCardScan");
    },
  },

  watch: {
    // 监听父级传递过来的props--field
    field: {
      handler: function (val) {
        let key = val.options["config"];
        this.optionsInfo = val.options[key];
        // this.backcountdown = this.optionsInfo.backCountdown * 1000 || 120000;
        // this.$store.dispatch("setReportStepInfo", this.optionsInfo.step);
      },
      deep: true,
      immediate: true,
    },

    // 步-距
    // "optionsInfo.step": {
    //   handler: function (val) {
    //     const self = this;
    //     this.optionsInfo.step = parseInt(val);
    //     // 步骤过程发生变化--执行时间的倒计时
    //     if (parseInt(val) === 1) {
    //       // self.backcountdown = (self.getBackcountdown);
    //       self.timer = setInterval(() => {
    //         self.backcountdown = self.backcountdown - 1000;
    //         // 到达时间--返回首页-清空定时器
    //         if (self.backcountdown <= 0) {
    //           self.handleBack();
    //           clearInterval(self.timer);
    //         }
    //       }, 1000);
    //     } else {
    //       clearInterval(self.timer);
    //     }
    //   },
    //   deep: true,
    //   immediate: true,
    // },
  },

  destroyed() {
    this.unregisterFromRefList();
    this.$store.dispatch("setReportStepInfo", 0);
  },
};
</script>

<style lang="scss" scoped>
.overFillHidden {
  width: 0.1px;
  height: 0.1px;
  overflow: hidden;
}
.scene-report-pick-info-widget {
  font-size: 60px;
  overflow: hidden;
  background: transparent !important;
  position: relative;

  .pick-area-wrap {
    height: 100%;
    padding: 40px 30px;
    box-sizing: border-box;

    .pick-area {
      display: flex;
      height: 100%;
      padding: 40px 0;
      flex-direction: column;
      box-sizing: border-box;
      border-radius: 60px;
      overflow: hidden;
      background: white;

      .top-header {
        line-height: 84px;
        font-style: normal;
        //margin-bottom: 20px;
        text-align: center;
        font-weight: 500;
        font-size: 60px;
        color: #333333;
        padding: 12px 114px;
        display: flex;
        justify-content: flex-end;

        .left {
          display: inline-block;
          width: 66px;
          height: 66px;
          align-self: center;
          position: relative;
          top: -20px;

          img {
            height: 100%;
            width: 100%;
            align-items: center;
            align-self: center;
          }
        }

        .middle {
          width: calc(100% - 66px - 66px);
          margin: 0 32px;
          font-size: 60px;
          color: #333333;
          line-height: 1.4;
          font-weight: normal;
          align-self: self-end;
          text-align: center;
          font-style: normal;
          @include fontWeight();
        }

        .right {
          display: inline-block;
          width: 66px;
          height: 66px;
          position: relative;
          top: -20px;
          align-items: center;
          align-self: center;

          img {
            height: 100%;
            width: 100%;
            align-items: center;
            align-self: center;
          }
        }
      }

      .second {
        height: 86px;
        //min-height: 86px;
        padding-left: 276px;
        padding-right: 276px;

        .el-input {
          height: 100%;
        }
      }

      .button-wrap {
        flex-basis: 744px;
        width: 100%;
        text-align: center;

        .bottom {
          display: flex;
          align-items: center;
          align-self: center;
          justify-content: center;
          padding-bottom: 40px;

          .button {
            border-radius: 100px;
            line-height: 125px;
            font-size: 80px;
            padding: 0 276px;
            cursor: pointer;
            color: #e8e8e8;
            background: #0059b3;
            //box-shadow: 0 9px 0 rgba(0, 89, 179, 0.5), 0 9px 25px rgba(0, 89, 179, 0.06);
          }
        }
      }
    }
  }
}
</style>
